package com.gitee.wsl.compose.ui.section.lazylist.item

import androidx.compose.foundation.interaction.MutableInteractionSource
import androidx.compose.foundation.layout.Column
import androidx.compose.foundation.layout.size
import androidx.compose.material.icons.Icons
import androidx.compose.material3.Icon
import androidx.compose.runtime.Composable
import androidx.compose.ui.Modifier
import androidx.compose.ui.unit.Dp
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.ext.ui.copy
import com.gitee.wsl.compose.icon.outlined.ChevronDown
import com.gitee.wsl.compose.icon.outlined.ChevronUp
import com.gitee.wsl.compose.ui.contextmenu.CupertinoDropdownMenu
import com.gitee.wsl.compose.ui.contextmenu.CupertinoDropdownMenuDefaults
import com.gitee.wsl.compose.ui.contextmenu.CupertinoMenuScope
import com.gitee.wsl.compose.ui.section.CupertinoSectionDefaults
import com.gitee.wsl.compose.ui.section.CupertinoSectionTokens
import com.gitee.wsl.compose.ui.section.LabelCaption
import com.gitee.wsl.compose.ui.section.lazylist.LazySectionScope


/**
 * Popup dropdown menu with [title], optional [icon] and [selectedLabel].
 *
 * @param onClick action performed on label click.
 * @param key optional key for item.
 * @param enabled if label is clickable.
 * @param icon icon displayed at the start of this item. [CupertinoLinkIcon] is often used for it.
 * @param dividerPadding start divider padding. By default inferred from presence of [icon].
 * @param onClickLabel semantics description of this label. Should be the same text as in [title].
 * @param interactionSource label interaction source.
 * @param selectedLabel content displayed before the label chevron.
 * @param title label title.
 *
 * @see CupertinoLinkIcon
 * @see switch
 * */

fun LazySectionScope.dropdownMenu(
    expanded: Boolean,
    onDismissRequest : () -> Unit,
    onClick: () -> Unit,
    modifier: Modifier = Modifier,
    key: Any? = null,
    enabled: Boolean = true,
    icon: (@Composable () -> Unit)? = null,
    width: Dp = CupertinoDropdownMenuDefaults.SmallWidth,
    dividerPadding: Dp = if (icon != null)
        CupertinoSectionDefaults.DividerPaddingWithIcon
    else CupertinoSectionDefaults.DividerPadding,
    onClickLabel: String? = null,
    interactionSource: MutableInteractionSource? = null,
    selectedLabel : @Composable () -> Unit = {},
    title: @Composable () -> Unit,
    content : @Composable CupertinoMenuScope.() -> Unit
) = LabelWithCustomChevron(
    chevron = {
        LabelCaption(selectedLabel)
        Column {
            val p = CupertinoDropdownMenuDefaults.PaddingValues
            CupertinoDropdownMenu(
                modifier = modifier,
                width = width,
                paddingValues = p.copy(
                    top = p.calculateTopPadding() +
                            CupertinoSectionTokens.VerticalPadding,
                    bottom = p.calculateBottomPadding() +
                            CupertinoSectionTokens.VerticalPadding,
                    start = 0.dp,
                    end = 0.dp
                ),
                expanded = expanded,
                onDismissRequest = onDismissRequest,
                content = content
            )
            Icon(
                imageVector = Icons.Outlined.ChevronUp,
                contentDescription = null,
                //tint = CupertinoTheme.colorScheme.tertiaryLabel,
                modifier = Modifier
                    .size(8.dp)
            )
            Icon(
                imageVector = Icons.Outlined.ChevronDown,
                contentDescription = null,
                //tint = CupertinoTheme.colorScheme.tertiaryLabel,
                modifier = Modifier
                    .size(8.dp)
            )
        }
    },
    onClick = onClick,
    key = key,
    enabled = enabled,
    icon = icon,
    dividerPadding = dividerPadding,
    onClickLabel = onClickLabel,
    interactionSource = interactionSource,
    title = title,
)